import numpy as np
import pandas as pd
import seaborn as sns
import sklearn
import matplotlib.pyplot as plt


from sklearn.datasets import load_iris
X, y = load_iris(return_X_y=True, as_frame=True)
#划分数据集
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X,y, test_size=0.2,random_state=20)
#查看X_train的直方图效果
X_train.hist(bins=30, figsize=(20,15))
plt.show()

sns.scatterplot(data=X_train, x='sepal length (cm)', y='sepal width (cm)', hue=y_train)
plt.show()
sns.scatterplot(data=X_train, x='petal length (cm)', y='petal width (cm)', hue=y_train)
plt.show()

from sklearn.pipeline import Pipeline
from sklearn.preprocessing import MinMaxScaler
scale_pipe = Pipeline([ ('scaler', MinMaxScaler())])
X_train_scaled = scale_pipe.fit_transform(X_train)

from sklearn.svm import LinearSVC
#线性svc
lin_svc = LinearSVC()
lin_svc.fit(X_train_scaled, y_train)


from sklearn.metrics import accuracy_score
lin_pred = lin_svc.predict(X_train_scaled)
accuracy = accuracy_score(y_train, lin_pred)
# 打印准确率
print(f"训练集上的准确率: {accuracy:.2f}")


